package com.yvan.serverless.jdbc.util;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.yvan.platform.StringUtils;
import com.yvan.serverless.jdbc.DaoParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DaoExecuterDeleteUtils {

    public static String getDeleteAnySql(String tableName, Map<String, Object> wheres) {
        if (StringUtils.isBlank(tableName)) {
            throw new RuntimeException("TableName cannot be empty");
        }
        if (wheres == null || wheres.size() <= 0) {
            throw new RuntimeException("Wheres cannot be empty");
        }
        List<String> whereFieldList = new ArrayList<>();
        for (String key : wheres.keySet()) {
            whereFieldList.add(key + "=?");
        }
        String sql = "delete from " + tableName + " where " + Joiner.on(" and ").join(whereFieldList);
        return sql;
    }

    public static List<DaoParam> getDeleteAnyParams(Map<String, Object> wheres) {
        List<DaoParam> values = Lists.newArrayList();
        for (String key : wheres.keySet()) {
            DaoParam param = new DaoParam(key, wheres.get(key));
            values.add(param);
        }
        return values;
    }

    public static String getDeleteByIdsSql(String tableName, String idField, List<Object> ids) {
        if (StringUtils.isBlank(tableName)) {
            throw new RuntimeException("TableName cannot be empty");
        }
        if (ids == null || ids.size() <= 0) {
            throw new RuntimeException("Wheres cannot be empty");
        }
        List<String> whereFieldList = new ArrayList<>();
        for (Object id : ids) {
            whereFieldList.add("?");
        }
        String sql = "delete from " + tableName + " where " + idField + " in (" + Joiner.on(",").join(whereFieldList) + ")";
        return sql;
    }

    public static List<DaoParam> getDeleteByIdsParams(String idField, List<Object> ids) {
        List<DaoParam> values = Lists.newArrayList();
        for (Object id : ids) {
            DaoParam param = new DaoParam(idField, id);
            values.add(param);
        }
        return values;
    }


}
